<template>
  <Teleport to="body">
    <Transition name="modal">
      <div v-if="show" class="apply-modal" @click.self="handleClose">
        <div class="modal-container">
          <button class="close-btn" @click="handleClose">
            <i class="ri-close-line"></i>
          </button>

          <div class="modal-content">
            <div class="modal-header">
              <h3 class="modal-title">申请友链</h3>
              <p class="modal-desc">请填写以下信息，申请通过后将展示在友链页面</p>
            </div>

            <form class="apply-form" @submit.prevent="handleSubmit">
              <div class="form-group">
                <label class="form-label">
                  <i class="ri-user-line"></i>
                  <span>博客名称</span>
                </label>
                <input
                  v-model="form.name"
                  type="text"
                  placeholder="输入您的博客名称"
                  required
                  class="form-input"
                />
              </div>

              <div class="form-group">
                <label class="form-label">
                  <i class="ri-link"></i>
                  <span>博客链接</span>
                </label>
                <input
                  v-model="form.url"
                  type="url"
                  placeholder="https://yourblog.com"
                  required
                  class="form-input"
                />
              </div>

              <div class="form-group">
                <label class="form-label">
                  <i class="ri-image-line"></i>
                  <span>头像链接</span>
                </label>
                <input
                  v-model="form.avatar"
                  type="url"
                  placeholder="https://yourblog.com/avatar.jpg"
                  required
                  class="form-input"
                />
              </div>

              <div class="form-group">
                <label class="form-label">
                  <i class="ri-file-text-line"></i>
                  <span>博客描述</span>
                </label>
                <input
                  v-model="form.description"
                  type="text"
                  placeholder="一句话介绍您的博客"
                  required
                  maxlength="50"
                  class="form-input"
                />
              </div>

              <button type="submit" class="submit-btn">
                <i class="ri-send-plane-line"></i>
                <span>提交申请</span>
              </button>
            </form>

            <div class="my-info">
              <h4 class="info-title">我的信息</h4>
              <div class="info-list">
                <div class="info-item">
                  <span class="info-label">名称：</span>
                  <span class="info-value">递归茶馆</span>
                </div>
                <div class="info-item">
                  <span class="info-label">链接：</span>
                  <span class="info-value">https://blog.example.com</span>
                </div>
                <div class="info-item">
                  <span class="info-label">头像：</span>
                  <span class="info-value">https://blog.example.com/avatar.jpg</span>
                </div>
                <div class="info-item">
                  <span class="info-label">描述：</span>
                  <span class="info-value">前端开发者的个人博客</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  show: {
    type: Boolean,
    default: false,
  },
})

const emit = defineEmits(['close', 'submit'])

const form = ref({
  name: '',
  url: '',
  avatar: '',
  description: '',
})

const handleClose = () => {
  emit('close')
}

const handleSubmit = () => {
  emit('submit', { ...form.value })
  // 重置表单
  form.value = {
    name: '',
    url: '',
    avatar: '',
    description: '',
  }
  handleClose()
}

// 监听弹窗显示状态，控制 body 滚动
watch(
  () => props.show,
  (newVal) => {
    if (newVal) {
      document.body.style.overflow = 'hidden'
    } else {
      document.body.style.overflow = ''
    }
  },
  { immediate: true },
)
</script>

<style lang="scss" scoped>
.apply-modal {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(8px);
  padding: 20px;

  .modal-container {
    position: relative;
    width: 100%;
    max-width: 520px;
    max-height: 90vh;
    background: var(--bg-base);
    border-radius: 20px;
    overflow: hidden;
    box-shadow: var(--shadow-dark);
  }

  .close-btn {
    position: absolute;
    top: 20px;
    right: 20px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--bg-light);
    border: none;
    color: var(--text-primary);
    font-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 10;
    transition: var(--transition-base);

    &:hover {
      background: var(--color-primary);
      color: #fff;
      transform: rotate(90deg);
    }
  }

  .modal-content {
    padding: 40px;
    overflow-y: auto;
    max-height: 90vh;
  }

  .modal-header {
    margin-bottom: 32px;
    text-align: center;

    .modal-title {
      font-size: 28px;
      font-weight: 700;
      color: var(--text-primary);
      margin-bottom: 8px;
      background: linear-gradient(90deg, var(--color-primary), var(--color-success));
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
    }

    .modal-desc {
      font-size: 14px;
      color: var(--text-regular);
      line-height: 1.6;
    }
  }

  .apply-form {
    margin-bottom: 32px;

    .form-group {
      margin-bottom: 24px;

      .form-label {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 14px;
        font-weight: 600;
        color: var(--text-primary);
        margin-bottom: 10px;

        i {
          color: var(--color-primary);
          font-size: 16px;
        }
      }

      .form-input {
        width: 100%;
        height: 48px;
        padding: 0 16px;
        border: 1px solid var(--border-base);
        border-radius: 12px;
        background: var(--bg-base);
        color: var(--text-primary);
        font-size: 15px;
        transition: var(--transition-base);

        &:focus {
          outline: none;
          border-color: var(--color-primary);
          box-shadow: 0 0 0 3px rgba(81, 138, 255, 0.1);
        }

        &::placeholder {
          color: var(--text-placeholder);
        }
      }
    }

    .submit-btn {
      width: 100%;
      height: 52px;
      border: none;
      border-radius: 12px;
      background: linear-gradient(90deg, var(--color-primary), var(--color-success));
      color: #fff;
      font-size: 16px;
      font-weight: 600;
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 8px;
      cursor: pointer;
      transition: var(--transition-base);
      box-shadow: 0 4px 12px rgba(81, 138, 255, 0.3);

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 16px rgba(81, 138, 255, 0.4);
      }

      i {
        font-size: 18px;
      }
    }
  }

  .my-info {
    padding: 24px;
    background: var(--bg-light);
    border-radius: 12px;
    border: 1px solid var(--border-lighter);

    .info-title {
      font-size: 16px;
      font-weight: 600;
      color: var(--text-primary);
      margin-bottom: 16px;
      padding-left: 12px;
      border-left: 3px solid var(--color-primary);
    }

    .info-list {
      display: flex;
      flex-direction: column;
      gap: 12px;

      .info-item {
        display: flex;
        font-size: 14px;

        .info-label {
          color: var(--text-regular);
          width: 60px;
          flex-shrink: 0;
        }

        .info-value {
          color: var(--text-primary);
          word-break: break-all;
        }
      }
    }
  }
}

// 过渡动画
.modal-enter-active,
.modal-leave-active {
  transition: opacity 0.3s ease;
}

.modal-enter-active .modal-container,
.modal-leave-active .modal-container {
  transition:
    transform 0.3s ease,
    opacity 0.3s ease;
}

.modal-enter-from,
.modal-leave-to {
  opacity: 0;

  .modal-container {
    transform: scale(0.9) translateY(20px);
    opacity: 0;
  }
}

@media (max-width: 768px) {
  .apply-modal {
    padding: 10px;

    .modal-container {
      max-height: 95vh;
      border-radius: 16px;
    }

    .close-btn {
      top: 12px;
      right: 12px;
      width: 36px;
      height: 36px;
      font-size: 20px;
    }

    .modal-content {
      padding: 24px 20px;
    }

    .modal-header {
      .modal-title {
        font-size: 24px;
      }
    }
  }
}
</style>
